
<template>
  <div class="contract">
    <el-form v-show="showSearch" ref="queryParams" :model="queryParams" size="small" :inline="true" label-width="110px">
      <el-form-item label="员工编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入员工编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入员工姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="证件号码" prop="cardNum">
        <el-input v-model="queryParams.cardNum" placeholder="请输入证件号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="岗位" prop="selectJob">
        <el-input v-model="queryParams.selectJob" placeholder="请输入岗位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择" clearable @keyup.enter.native="handleQuery">
          <el-option
            key="0"
            label="审核中"
            value="0"
          />
          <el-option
            key="1"
            label="已通过"
            value="1"
          />
          <el-option
            key="2"
            label="未通过"
            value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryParams')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="employeeList" stripe>
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="员工编号" prop="code" align="center" />
      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="cardNum" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormatter" />
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          {{ getEduName(scope.row.education) }}
        </template>
      </el-table-column>
      <el-table-column label="户籍地址" align="center" prop="domicileAddress" />
      <el-table-column label="岗位" align="center" prop="job" />
      <el-table-column label="生效日期" align="center" prop="insuranceStart" />
      <el-table-column label="失效日期" align="center" prop="insuranceEnd" />
      <el-table-column prop="status" label="审核状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditStatus === '0'" type="primary">审核中</el-tag>
          <el-tag v-else-if="scope.row.auditStatus === '1'" type="success">已通过</el-tag>
          <el-tag v-else-if="scope.row.auditStatus === '2'" type="danger">未通过</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="审核拒绝原因" align="center" prop="refuseRemark" />-->
      <!--      <el-table-column label="删除状态" align="center" prop="delFlag" />-->
      <!--      <el-table-column label="参与付款情况" align="center" prop="paymentStatus" >-->
      <!--        <template slot-scope="scope">-->
      <!--          <span v-if="scope.row.paymentStatus == '0'">未参与付款</span>-->
      <!--          <span v-if="scope.row.paymentStatus == '1'">已参与付款</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="参与结算情况" align="center" prop="settlementStatus" >-->
      <!--        <template slot-scope="scope">-->
      <!--          <span v-if="scope.row.settlementStatus == '0'">未参与结算</span>-->
      <!--          <span v-if="scope.row.settlementStatus == '1'">已参与结算</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-if="scope.row.auditStatus !== '1' && openStatus === 1">修改信息</el-button>
          <el-button size="mini" type="text" icon="el-icon-circle-close" @click="handleRevocation(scope.row)" v-if="scope.row.auditStatus !== '1' && openStatus === 1">撤保</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination :current-page="currentPage" :page-sizes="pageSizes" :page-size="pageSize" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>

    <!-- 添加或修改投保人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--        <el-form-item label="员工编号" prop="code">-->
        <!--          <el-input v-model="form.code" placeholder="请输入员工编号" />-->
        <!--        </el-form-item>-->

        <el-form-item label="所在单位" prop="companyId">
          <el-input v-model="form.secondName" placeholder="请输入所在单位" :disabled="true" />
        </el-form-item>

        <el-form-item label="所在合同" prop="agreementId">
          <el-select v-model="form.agreementId" placeholder="请选择所在合同" clearable filterable disabled="true">
            <el-option v-for="item in allCompanyAgreementList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入员工姓名" />
        </el-form-item>

        <el-form-item label="学历" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历" clearable>
            <el-option v-for="dict in eduList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="cardNum">
          <el-input v-model="form.cardNum" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
<!--        <el-form-item label="投保月份" prop="insuranceStart">-->
<!--          <el-date-picker v-model="form.insuranceStart" clearable type="month" value-format="yyyy-MM" placeholder="请选择投保月份" disabled="true"/>-->
<!--        </el-form-item>-->
        <el-form-item label="岗位" prop="job">
          <el-input v-model="form.job" placeholder="请输入岗位" />
        </el-form-item>
<!--        <el-form-item label="岗位类别" prop="category">-->
<!--          <el-input v-model="form.category" placeholder="请输入岗位类别" />-->
<!--        </el-form-item>-->
        <el-form-item label="移动电话" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入移动电话" />
        </el-form-item>
        <el-form-item label="户籍地址" prop="domicileAddress">
          <el-input v-model="form.domicileAddress" placeholder="请输入户籍地址" />
        </el-form-item>
        <el-form-item label="联系地址" prop="contactAddress">
          <el-input v-model="form.contactAddress" placeholder="请输入联系地址" />
        </el-form-item>
        <!--        <el-form-item label="删除状态" prop="delFlag">-->
        <!--          <el-input v-model="form.delFlag" placeholder="请输入删除状态" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  // insuranceList,
  // backListGroup,
  // waitAudit,
  // backList,
  employeeList,
  // employeeProjectList,
  revocation
  // revocationAll
} from '@/api/insure'

import {
  getEmployeeInfo,
  updateEmploeeInfo
} from '@/api/employee'

import {
  getDictData
} from '@/api/dict'

import {
  getAgreement
} from '@/api/contract'
import {checkOpen} from "@/api/setting";

export default {
  name: 'WaitAuditDetail',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 列表数据
      employeeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 学历
      eduList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        property: null,
        insuranceStart: null,
        insuranceEnd: null,
        cardType: null,
        cardNum: null,
        nation: null,
        age: null,
        sex: null,
        domicileType: null,
        education: null,
        companyId: null,
        mobilePhone: null,
        domicileAddress: null,
        status: null,
        job: null,
        category: null,
        contactAddress: null,
        agreementId: null,
        insucanceId: null,
        auditStatus: null,
        selectJob: null
      },
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50], // 每页条数
      pageSize: 10, // 每页条数
      total: 0, // 总条数
      // 表单参数
      form: {},
      rules: {
        agreementId: [
          { required: true, message: '所在合同不能为空', trigger: 'change' }
        ],
        name: [
          { required: true, message: '员工姓名不能为空', trigger: 'blur' }
        ],
        education: [
          { required: true, message: '学历不能为空', trigger: 'blur' }
        ],
        cardNum: [
          { required: true, message: '证件号码不能为空', trigger: 'blur' }
        ],
        nation: [
          { required: true, message: '名族不能为空', trigger: 'blur' }
        ],
        job: [
          { required: true, message: '岗位不能为空', trigger: 'blur' }
        ],
        // category: [
        //   { required: true, message: '岗位分类不能为空', trigger: 'blur' }
        // ],
        mobilePhone: [
          { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ]
      },
      allCompanyAgreementList: [],
      // 投保开始状态 0为未开启，1已开启
      openStatus: 0
    }
  },
  created() {
    if (this.$route.query.property) {
      this.queryParams.property = this.$route.query.property
    }
    if (this.$route.query.companyId) {
      this.queryParams.companyId = this.$route.query.companyId
    }
    if (this.$route.query.insuranceStart) {
      this.queryParams.insuranceMonth = this.$route.query.insuranceStart
    }
    if (this.$route.query.agreementId) {
      this.queryParams.agreementId = this.$route.query.agreementId
    }
    if (this.$route.query.insucanceId) {
      this.queryParams.insucanceId = this.$route.query.insucanceId
    }
    this.getList()
    this.getEduList()
    this.getCompanyAgreementList()
    this.checkOpenMethod()
  },
  methods: {
    getList() {
      this.loading = true
      employeeList(this.queryParams).then(response => {
        this.employeeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery(formName) {
      this.$refs[formName].resetFields()
      this.handleQuery()
    },

    // 每页条数改变
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    },
    sexFormatter(row) {
      switch (row.sex) {
        case '0':
          return '男'
        case '1':
          return '女'
      }
    },

    getEduList() {
      const data = { 'dictType': 'education' }
      getDictData(data).then(response => {
        this.eduList = response.data
      })
    },

    getEduName(value) {
      const dict = this.eduList.find(item => item?.dictValue === value)
      if (dict) {
        return dict.dictLabel
      }
    },
    // 撤保按钮
    handleRevocation(row) {
      this.$confirm('确认撤回' + row.name + '' + row.insuranceStart + '的保单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.revocationMethod(row.id)
      }).catch(() => {

      })
    },

    // 撤保方法
    revocationMethod(id) {
      revocation(id).then(response => {
        if (response.code === 200) {
          this.$message.success('撤保成功')
          this.getList()
        }
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        property: null,
        insuranceStart: null,
        insuranceEnd: null,
        cardType: null,
        cardNum: null,
        nation: null,
        age: null,
        sex: null,
        domicileType: null,
        education: null,
        companyId: null,
        mobilePhone: null,
        domicileAddress: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        job: null,
        category: null,
        contactAddress: null
      }
      // this.$refs['form'].resetFields()
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getEmployeeInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改员工信息'
        if (this.form.hroCompany) {
          if (this.form.hroCompany.nameSecond) {
            this.form.secondName = this.form.hroCompany.nameSecond
          }
        }
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmploeeInfo(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 获取客户合同列表
    getCompanyAgreementList() {
      const q = {
        'status': '0'
      }
      getAgreement(q).then(response => {
        this.allCompanyAgreementList = response.data
      })
    },
    checkOpenMethod() {
      var that = this
      checkOpen().then(res => {
        if (res.code === 200 && res.data.status === 1) {
          that.openStatus = res.data.status
        }
      })
    }

  }
}

</script>

<style lang="scss" scoped>
.contract {
  padding: 20px;
  line-height: 1;

  .contract_filter {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>
